package app.project.directionlensproject.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.content.res.Configuration;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import app.project.directionlensproject.R;
import app.project.directionlensproject.model.Category;
import app.project.directionlensproject.util.Constants;

public class CategoryAdapter extends BaseAdapter {
	private ArrayList<Category> items;
	private LayoutInflater inflater;
	private Context mContext;

	public CategoryAdapter(Context context) {
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mContext = context;
		initData();
	}

	public CategoryAdapter(Context context, ArrayList<Category> listCate) {
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mContext = context;
		items = new ArrayList<Category>();
		items.add(new Category(Constants.ID_C_ALL, context.getString(R.string.all), R.drawable.ic_all));
		items.addAll(listCate);
	}

	private void initData() {
		items = new ArrayList<Category>();
		items.add(new Category(Constants.ID_C_ALL, "Tất cả", R.drawable.ic_all));
		items.add(new Category(Constants.ID_C_BANK, "Ngân hàng", R.drawable.ic_bank));
		items.add(new Category(Constants.ID_C_BUS, "Vận chuyển", R.drawable.ic_bus));
		items.add(new Category(Constants.ID_C_KHAN_CAP, "Khẩn cấp", R.drawable.ic_emergency));
		items.add(new Category(Constants.ID_C_GIAI_TRI, "Giải trí", R.drawable.ic_entertaiment));
		items.add(new Category(Constants.ID_C_FOOD, "Nhà hàng", R.drawable.ic_food));
		items.add(new Category(Constants.ID_C_FUEL, "Trạm xăng", R.drawable.ic_fuel));
		items.add(new Category(Constants.ID_C_HOTEL, "Khách sạn", R.drawable.ic_hotel));
		items.add(new Category(Constants.ID_C_SCHOOL, "Trường học", R.drawable.ic_school));
		items.add(new Category(Constants.ID_C_SHOP, "Mua sắm", R.drawable.ic_shoping));
		items.add(new Category(Constants.ID_C_OTHER, "Khác", R.drawable.ic_other));
	}

	public int getCount() {
		return items.size();
	}

	public Category getItem(int position) {
		return items.get(position);
	}

	public long getItemId(int arg0) {
		return items.get(arg0).categoryId;
	}

	public View getView(int position, View convertView, ViewGroup arg2) {
		Category c = getItem(position);

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_category, null);
		}

		TextView tv = (TextView) convertView.findViewById(R.id.tv_name);
		Configuration loc = new Configuration(mContext.getResources().getConfiguration());
		String code = loc.locale.getLanguage();
		if (c.categoryId == 0) {
			if (code.equals("vi")) {
				tv.setText("Tất cả");
			} else
				tv.setText("All");
		} else {
			int start = c.name.indexOf('*');
			String vi = c.name.substring(0, start);
			String en = c.name.substring(start+1, c.name.length());
			if (code.equals("vi"))
				tv.setText(vi);
			else
				tv.setText(en);
		}

		ImageView imv = (ImageView) convertView.findViewById(R.id.imv_ic);
		if (c.image != null) {
			imv.setImageBitmap(c.getBitmap());
		} else
			imv.setImageResource(c.imageId);

		return convertView;
	}

}
